/**
 * AE Animated Object Shared Texture.
 *
 * Released under Apache License, Version 2.0.
 */

package bsp.andengine;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.layer.Layer;
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import android.content.Context;

public class AEAnimatedObjectSharedTexture
{
	protected AnimatedSprite mSprite;
	protected AETextureObject mTexture;
	protected TiledTextureRegion mTextureRegion;
	protected Engine mEngine;
	protected Scene mScene;
	protected Layer mLayer;
	protected boolean bAttachedToLayer, bCleanUpDone;
	
	public AEAnimatedObjectSharedTexture()
	{
		mSprite = null;
		mTexture = null;
		mTextureRegion = null;
		mEngine = null;
		mScene = null;
		mLayer = null;
		bAttachedToLayer = false;
		bCleanUpDone = false;
	}
	
	public void createTextureRegion(Context context, int resourceID, AETextureObject texture, int posX, int posY, int tileCols, int tileRows)
	{
		mTexture = texture;
		mTextureRegion = TextureRegionFactory.createTiledFromResource(mTexture.getHandle(), context, resourceID, posX, posY, tileCols, tileRows);
	}
	
	public void createTextureRegionFromAsset(Context context, String name, AETextureObject texture, int posX, int posY, int tileCols, int tileRows)
	{
		mTexture = texture;
		mTextureRegion = TextureRegionFactory.createTiledFromAsset(mTexture.getHandle(), context, name, posX, posY, tileCols, tileRows);
	}
	
	public void loadTexture()
	{
		mTexture.load();
	}
	
	public void unloadTexture()
	{
		mTexture.unload();
	}
	
	public void createSprite(Scene scene, Layer layer, float x, float y)
	{
		mScene = scene;
		mLayer = layer;
		mSprite = new AnimatedSprite(x, y, mTextureRegion);
	}
	
	public void createSprite(Scene scene, Layer layer, float x, float y, int wd, int ht)
	{
		mScene = scene;
		mLayer = layer;
		mSprite = new AnimatedSprite(x, y, wd, ht, mTextureRegion);
	}
	
	public void registerUpdateHandler(IUpdateHandler updateHandler)
	{
		mSprite.registerUpdateHandler(updateHandler);
	}
	
	public void addSpriteToScene()
	{
		mScene.attachChild(mSprite);
		bAttachedToLayer = true;
	}
	
	public void removeSpriteFromScene()
	{
		if ( bAttachedToLayer == true ) {
			if ( mSprite != null ) {
				mScene.detachChild(mSprite);
			}
			bAttachedToLayer = false;
		}
	}
	
	public Layer getLayer()
	{
		return mLayer;
	}
	
	public AnimatedSprite getSprite()
	{
		return mSprite;
	}
	
	public Texture getTexture()
	{
		return mTexture.getHandle();
	}
	
	public TiledTextureRegion getTextureRegion()
	{
		return mTextureRegion;
	}
	
	public Rectangle getRectangle()
	{
		return new Rectangle(mSprite.getX(), mSprite.getY(), mSprite.getWidth(), mSprite.getHeight());
	}
	
	public void setPosition(float x, float y)
	{
		mSprite.setPosition(x, y);
	}
	
	public void setPositionCentered(float x, float y)
	{
		float nx = x - (mSprite.getWidth() * 0.5f);
		float ny = y - (mSprite.getHeight() * 05.f);
		mSprite.setPosition(nx, ny);
	}
	
	/*
	public void animate(long pFrameDurationEach)
	{
		mSprite.animate(pFrameDurationEach);
	}
	*/
	
	public void cleanUp()
	{
		if ( bCleanUpDone == false ) {
			removeSpriteFromScene();
			
			mSprite = null;
			
			if ( mTexture != null ) {
				mTexture = null;
				mTextureRegion = null;
			}
		} else {
			bCleanUpDone = true;
		}
	}
	
	protected void finalize() throws Throwable
	{
		cleanUp();
	}
}

/* end */

